package edu.stu.mall.fragment.hot_key

import android.view.View
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.bottomsheet.BottomSheetDialog
import edu.mall.base.BaseFragment
import edu.stu.mall.BR
import edu.stu.mall.R
import edu.stu.mall.adapter.ProductCategoryAdapter
import edu.stu.mall.adapter.ProductCategoryChildrenAdapter
import edu.stu.mall.databinding.FragmentHotKeyBinding


class FragHotKey : BaseFragment<FragmentHotKeyBinding, FragHotKeyViewModel>() {
//    private val productCategoryAdapter = ProductCategoryAdapter()
    // 使用by lazy来初始化适配器，以避免重复创建。
    private val productCategoryAdapter by lazy { ProductCategoryAdapter() }
    private val productCategoryChildrenAdapter by lazy { ProductCategoryChildrenAdapter() }
//    private val productCategoryChildrenListAdapter by lazy { ProductCategoryChildrenListAdapter() }
    override fun getLayoutId(): Int = R.layout.fragment_hot_key

    override fun getViewModelId(): Int = BR.hotKeyVm


    override fun initViewData() {
        binding?.btn?.setOnClickListener { showBottomSheetDialog() }
        setupRecyclerView()
        observeProductCategories()


    }
    private fun setupRecyclerView() {
        //        垂直布局
        binding?.rlvName?.layoutManager = LinearLayoutManager(context)
        binding?.rlvName?.adapter = productCategoryAdapter
        //        垂直布局
        binding?.rlvChildren?.layoutManager = LinearLayoutManager(context)
        binding?.rlvChildren?.adapter = productCategoryChildrenAdapter



    }
    private fun observeProductCategories() {
        viewModel?.productCategoryListData?.observe(viewLifecycleOwner) { it ->
            productCategoryAdapter.setData(it)
            productCategoryChildrenAdapter.setData(it)

            it?.forEach {
//                productCategoryChildrenListAdapter.setData(it.children)


            }
        }



    }
    private fun showBottomSheetDialog() {
        // 使用 requireContext() 获取正确的 Context
        val bottomSheetDialog = BottomSheetDialog(requireContext())

        // 为 BottomSheetDialog 设置布局
        val view: View = layoutInflater.inflate(R.layout.bottom_sheet_layout, null)
        bottomSheetDialog.setContentView(view)

        // 获取布局中的 Button，并设置点击事件
        val btnClose = view.findViewById<Button>(R.id.btnClose)
        btnClose.setOnClickListener {
            // 关闭 BottomSheetDialog
            bottomSheetDialog.dismiss()
        }

        // 显示 BottomSheetDialog
        bottomSheetDialog.show()
    }


}